package com.liyuxiang.graph.service;

import com.liyuxiang.graph.entity.EnhancedAiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Arrays;

/**
 * 快捷选择服务 - 根据症状询问阶段提供预设选项
 */
@Service
public class QuickSelectionService {
    
    private static final Logger logger = LoggerFactory.getLogger(QuickSelectionService.class);
    
    /**
     * 根据AI响应和上下文生成快捷选择选项
     */
    public EnhancedAiResponse enhanceWithQuickOptions(String aiResponse, 
                                                      Map<String, Object> symptoms, 
                                                      int questionCount,
                                                      String intentType) {
        
        EnhancedAiResponse enhanced = new EnhancedAiResponse();
        enhanced.setResponse(aiResponse);
        
        // 分析AI响应，判断是否需要快捷选择
        String lowerResponse = aiResponse.toLowerCase();
        
        try {
            // 疼痛性质相关
            if (containsKeywords(lowerResponse, Arrays.asList("持续", "间歇", "疼痛性质", "怎么疼"))) {
                enhanced = createPainTypeOptions(aiResponse);
            }
            // 疼痛程度相关
            else if (containsKeywords(lowerResponse, Arrays.asList("严重", "程度", "疼得", "多疼"))) {
                enhanced = createPainLevelOptions(aiResponse);
            }
            // 持续时间相关
            else if (containsKeywords(lowerResponse, Arrays.asList("多久", "时间", "什么时候开始", "持续"))) {
                enhanced = createDurationOptions(aiResponse);
            }
            // 疼痛部位相关
            else if (containsKeywords(lowerResponse, Arrays.asList("位置", "哪里疼", "部位", "具体在"))) {
                enhanced = createLocationOptions(aiResponse);
            }
            // 伴随症状相关
            else if (containsKeywords(lowerResponse, Arrays.asList("伴随", "其他症状", "还有", "同时"))) {
                enhanced = createAccompanyingOptions(aiResponse);
            }
            // 诱发因素相关
            else if (containsKeywords(lowerResponse, Arrays.asList("诱因", "什么引起", "之前做了", "因为"))) {
                enhanced = createTriggerOptions(aiResponse);
            }
            // 用药史相关
            else if (containsKeywords(lowerResponse, Arrays.asList("用药", "服药", "吃药", "治疗"))) {
                enhanced = createMedicationOptions(aiResponse);
            }
            // 默认情况 - 纯文本
            else {
                enhanced.setResponseType(EnhancedAiResponse.ResponseType.TEXT);
                enhanced.setShowTextInput(true);
            }
            
            logger.info("生成快捷选择选项 - 类型: {}, 选项数量: {}", 
                       enhanced.getResponseType(), 
                       enhanced.getQuickOptions() != null ? enhanced.getQuickOptions().size() : 0);
            
        } catch (Exception e) {
            logger.error("生成快捷选择选项失败: {}", e.getMessage());
            // 降级到纯文本模式
            enhanced.setResponseType(EnhancedAiResponse.ResponseType.TEXT);
            enhanced.setShowTextInput(true);
        }
        
        return enhanced;
    }
    
    /**
     * 创建疼痛性质选项
     */
    private EnhancedAiResponse createPainTypeOptions(String aiResponse) {
        EnhancedAiResponse response = new EnhancedAiResponse();
        response.setResponse(aiResponse);
        response.setResponseType(EnhancedAiResponse.ResponseType.MIXED);
        response.setSelectionPrompt("请选择疼痛的性质：");
        response.setShowTextInput(false);
        
        response.setQuickOptions(Arrays.asList(
            new EnhancedAiResponse.QuickOption("持续性疼痛", "持续性疼痛", "primary", "⚡", true),
            new EnhancedAiResponse.QuickOption("间歇性疼痛", "间歇性疼痛", "primary", "📊", false),
            new EnhancedAiResponse.QuickOption("阵发性疼痛", "阵发性疼痛", "secondary", "🌪️", false),
            new EnhancedAiResponse.QuickOption("跳痛", "跳痛", "secondary", "💓", false),
            new EnhancedAiResponse.QuickOption("胀痛", "胀痛", "secondary", "🎈", false),
            new EnhancedAiResponse.QuickOption("刺痛", "刺痛", "secondary", "🗡️", false),
            new EnhancedAiResponse.QuickOption("其他", "其他类型的疼痛", "default", "✍️", false)
        ));
        
        return response;
    }
    
    /**
     * 创建疼痛程度选项
     */
    private EnhancedAiResponse createPainLevelOptions(String aiResponse) {
        EnhancedAiResponse response = new EnhancedAiResponse();
        response.setResponse(aiResponse);
        response.setResponseType(EnhancedAiResponse.ResponseType.MIXED);
        response.setSelectionPrompt("请选择疼痛程度：");
        response.setShowTextInput(false);
        
        response.setQuickOptions(Arrays.asList(
            new EnhancedAiResponse.QuickOption("轻微疼痛", "轻微疼痛，可以忍受", "success", "😐", false),
            new EnhancedAiResponse.QuickOption("中度疼痛", "中度疼痛，影响活动", "warning", "😣", true),
            new EnhancedAiResponse.QuickOption("严重疼痛", "严重疼痛，难以忍受", "danger", "😰", false),
            new EnhancedAiResponse.QuickOption("剧烈疼痛", "剧烈疼痛，无法忍受", "danger", "😱", false)
        ));
        
        return response;
    }
    
    /**
     * 创建持续时间选项
     */
    private EnhancedAiResponse createDurationOptions(String aiResponse) {
        EnhancedAiResponse response = new EnhancedAiResponse();
        response.setResponse(aiResponse);
        response.setResponseType(EnhancedAiResponse.ResponseType.MIXED);
        response.setSelectionPrompt("请选择持续时间：");
        response.setShowTextInput(false);
        
        response.setQuickOptions(Arrays.asList(
            new EnhancedAiResponse.QuickOption("刚开始", "刚开始疼，不到1小时", "primary", "🕐", false),
            new EnhancedAiResponse.QuickOption("几小时", "几个小时了", "primary", "🕕", true),
            new EnhancedAiResponse.QuickOption("1-2天", "1-2天了", "secondary", "📅", false),
            new EnhancedAiResponse.QuickOption("3-7天", "3-7天了", "warning", "📊", false),
            new EnhancedAiResponse.QuickOption("1-2周", "1-2周了", "warning", "📋", false),
            new EnhancedAiResponse.QuickOption("超过1个月", "超过1个月了", "danger", "📈", false),
            new EnhancedAiResponse.QuickOption("其他", "其他时间", "default", "✍️", false)
        ));
        
        return response;
    }
    
    /**
     * 创建疼痛部位选项
     */
    private EnhancedAiResponse createLocationOptions(String aiResponse) {
        EnhancedAiResponse response = new EnhancedAiResponse();
        response.setResponse(aiResponse);
        response.setResponseType(EnhancedAiResponse.ResponseType.MIXED);
        response.setSelectionPrompt("请选择疼痛的具体位置：");
        response.setShowTextInput(false);
        
        response.setQuickOptions(Arrays.asList(
            new EnhancedAiResponse.QuickOption("上腹部中央", "上腹部中央（胃部附近）", "primary", "🟢", true),
            new EnhancedAiResponse.QuickOption("右上腹", "右上腹部", "primary", "🔴", false),
            new EnhancedAiResponse.QuickOption("左上腹", "左上腹部", "primary", "🔵", false),
            new EnhancedAiResponse.QuickOption("下腹部", "下腹部", "secondary", "🟡", false),
            new EnhancedAiResponse.QuickOption("右下腹", "右下腹部", "secondary", "🟠", false),
            new EnhancedAiResponse.QuickOption("左下腹", "左下腹部", "secondary", "🟣", false),
            new EnhancedAiResponse.QuickOption("全腹", "整个肚子都疼", "warning", "⭕", false),
            new EnhancedAiResponse.QuickOption("其他", "其他部位", "default", "✍️", false)
        ));
        
        return response;
    }
    
    /**
     * 创建伴随症状选项
     */
    private EnhancedAiResponse createAccompanyingOptions(String aiResponse) {
        EnhancedAiResponse response = new EnhancedAiResponse();
        response.setResponse(aiResponse);
        response.setResponseType(EnhancedAiResponse.ResponseType.MIXED);
        response.setSelectionPrompt("请选择伴随的症状：（可多选）");
        response.setShowTextInput(true);
        
        response.setQuickOptions(Arrays.asList(
            new EnhancedAiResponse.QuickOption("无其他症状", "没有其他伴随症状", "success", "✅", true),
            new EnhancedAiResponse.QuickOption("恶心", "伴有恶心", "warning", "🤢", false),
            new EnhancedAiResponse.QuickOption("呕吐", "伴有呕吐", "warning", "🤮", false),
            new EnhancedAiResponse.QuickOption("发热", "伴有发热", "danger", "🌡️", false),
            new EnhancedAiResponse.QuickOption("腹泻", "伴有腹泻", "warning", "💩", false),
            new EnhancedAiResponse.QuickOption("便秘", "伴有便秘", "secondary", "🚫", false),
            new EnhancedAiResponse.QuickOption("头晕", "伴有头晕", "secondary", "😵", false)
        ));
        
        return response;
    }
    
    /**
     * 创建诱发因素选项
     */
    private EnhancedAiResponse createTriggerOptions(String aiResponse) {
        EnhancedAiResponse response = new EnhancedAiResponse();
        response.setResponse(aiResponse);
        response.setResponseType(EnhancedAiResponse.ResponseType.MIXED);
        response.setSelectionPrompt("请选择可能的诱发因素：");
        response.setShowTextInput(false);
        
        response.setQuickOptions(Arrays.asList(
            new EnhancedAiResponse.QuickOption("饮食相关", "吃了什么食物后开始疼", "primary", "🍽️", true),
            new EnhancedAiResponse.QuickOption("运动后", "运动后开始疼", "secondary", "🏃", false),
            new EnhancedAiResponse.QuickOption("工作劳累", "工作劳累后", "secondary", "💼", false),
            new EnhancedAiResponse.QuickOption("情绪激动", "生气或情绪激动后", "warning", "😤", false),
            new EnhancedAiResponse.QuickOption("受凉", "着凉受寒后", "primary", "🧊", false),
            new EnhancedAiResponse.QuickOption("无明显诱因", "突然就开始疼了", "default", "❓", false),
            new EnhancedAiResponse.QuickOption("其他", "其他原因", "default", "✍️", false)
        ));
        
        return response;
    }
    
    /**
     * 创建用药选项
     */
    private EnhancedAiResponse createMedicationOptions(String aiResponse) {
        EnhancedAiResponse response = new EnhancedAiResponse();
        response.setResponse(aiResponse);
        response.setResponseType(EnhancedAiResponse.ResponseType.MIXED);
        response.setSelectionPrompt("请选择用药情况：");
        response.setShowTextInput(true);
        
        response.setQuickOptions(Arrays.asList(
            new EnhancedAiResponse.QuickOption("未用药", "暂时没有吃任何药物", "success", "🚫", true),
            new EnhancedAiResponse.QuickOption("止痛药", "吃了止痛药（如布洛芬等）", "primary", "💊", false),
            new EnhancedAiResponse.QuickOption("胃药", "吃了胃药", "primary", "🥛", false),
            new EnhancedAiResponse.QuickOption("中成药", "吃了中成药", "secondary", "🌿", false),
            new EnhancedAiResponse.QuickOption("其他药物", "吃了其他药物", "default", "✍️", false)
        ));
        
        return response;
    }
    
    /**
     * 检查响应中是否包含关键词
     */
    private boolean containsKeywords(String text, List<String> keywords) {
        return keywords.stream().anyMatch(text::contains);
    }
}